//
//  TapView.m
//  LightControl
//
//  Created by 严丹 on 2025/8/18.
//

#import "TapView.h"
@interface TapView () <UIGestureRecognizerDelegate>

@end
@implementation TapView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupGestureRecognizers];
    }
    return self;
}
- (void)setupGestureRecognizers {
    // 启用用户交互
    self.userInteractionEnabled = YES;
    
    // 创建单击手势
    UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    singleTapGesture.numberOfTapsRequired = 1;
    singleTapGesture.delegate = self;
    [self addGestureRecognizer:singleTapGesture];
    
    // 创建双击手势
    UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    doubleTapGesture.numberOfTapsRequired = 2;
    doubleTapGesture.delegate = self;
    [self addGestureRecognizer:doubleTapGesture];
    
    // 确保双击手势优先识别
    [singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];
}

- (void)handleSingleTap:(UITapGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateRecognized) {
        if (self.singleTapBlock) {
            self.singleTapBlock();
        }
    }
}

- (void)handleDoubleTap:(UITapGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateRecognized) {
        if (self.doubleTapBlock) {
            self.doubleTapBlock();
        }
    }
}

// 实现代理方法，确保多个手势可以同时工作
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
